package com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ibm.websphere.asynchbeans.WorkException;
import com.motorola.examples.ServiceLocator;
import com.motorola.examples.processor.Processor;

public class WorkManagerServlet extends HttpServlet implements Servlet {
	
	public WorkManagerServlet(){
		super();
	}
	
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter pw = new PrintWriter(resp.getWriter(), true);
		pw.println("<html><head>");
		pw.println("<title></title>");
		pw.println("</head>");
		pw.println("<body>");
		pw.println("<h2>Start Thread: " +Thread.currentThread() + " of " + Thread.activeCount()+ "</h2>");
		try {
			System.out.println("Servlet Start Thread: " +Thread.currentThread() + " of " + Thread.activeCount());
			int i = 0; 
			while(i < 10){
				ServiceLocator.getWorkManager().startWork(new Processor());
				System.out.println("End Thread: " +Thread.currentThread() + " of " + Thread.activeCount());
				i++;
			}
			pw.println("<h2>End Thread: " +Thread.currentThread() + " of " + Thread.activeCount()+ "</h2>");
		} catch (WorkException e) {
			pw.println("<pre>");
			e.printStackTrace(pw);
			pw.println("</pre>");
		} catch (IllegalArgumentException e) {
			pw.println("<pre>");
			e.printStackTrace(pw);
			pw.println("</pre>");
		}

		
		pw.write("</body></html>");
	}
}
